package com.taobao.machinesystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taobao.machine_system.entity.vo.McPlanVo;
import com.taobao.machinesystem.common.Result;
import com.taobao.machinesystem.common.SystemConst;
import com.taobao.machinesystem.entity.McPlan1;
import com.taobao.machinesystem.entity.vo.McPlan0QueryVo;
import com.taobao.machinesystem.service.McPlan1Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 巡查计划表(从表) 前端控制器
 * </p>
 *
 * @author LiuJunCheng
 * @since 2022-04-22
 */
@Slf4j
@RestController
@RequestMapping("/machine_system/mc-plan1")
public class McPlan1Controller {

    @Autowired
    private McPlan1Service mcPlan1Service;

    /**
     * 分页查询 查看详情
     *
     * @param currentPage
     * @param size
     * @param mcPlan0QueryVo
     * @return Result
     */
    @GetMapping("/list/{currentPage}/{size}/")
    public Result listPlan1(
            @PathVariable("currentPage") Integer currentPage,
            @PathVariable("size") Integer size,
            McPlan0QueryVo mcPlan0QueryVo) {
        if (StringUtils.isEmpty(mcPlan0QueryVo.getPlanId())) {
            return Result.error().setMessage(SystemConst.NOT_NULL_PARAMETER+"ID不能为空");
        }

        Page<McPlan1> mcPlan1Page = mcPlan1Service.listMcPlan1(currentPage, size, mcPlan0QueryVo);

        return Result.ok()
                .setMessage(SystemConst.SELECT_SUCCESS)
                .setMapValue("total", mcPlan1Page.getTotal())
                .setMapValue("items", mcPlan1Page.getRecords());
    }

    @PutMapping("/update")
    public Result updateMcPlan1(@RequestBody McPlan1 mcPlan1){
        if (Objects.isNull(mcPlan1) ) {
            return Result.error().setMessage(SystemConst.NOT_NULL_PARAMETER);
        }

        boolean flag = mcPlan1Service.updateMcPlan1(mcPlan1);

        if (flag) {
            log.info("update plan0 success {}", mcPlan1.toString());
            return Result.ok().setMessage(SystemConst.UPDATE_SUCCESS);
        } else {
            log.error("update plan0 fail {}", mcPlan1.toString());
            return Result.error().setMessage(SystemConst.UPDATE_FAIL);
        }
    }

    /**
     *
     * @return
     */
    @GetMapping("/get/mcPlan1/id")
    public Result getMcPlan1Id(){
        List<McPlanVo> mcPlanVos = mcPlan1Service.getCurrentYearMcPlan1Ids();
        return Result.ok().setMessage(SystemConst.SELECT_SUCCESS).setMapValue("items",mcPlanVos);
    }
}

